package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.menu.Menu;
import com.gxuwz.medical.domain.role.Role;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.domain.user.UserBean;
/**
 * 登录验证控制模块
 * @author 演示
 *
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends BaseServlet {

	private static final Logger logger = LogManager.getLogger(LoginServlet.class);
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		login(request, response);
		
	}
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userid = request.getParameter("userid");
        String pwd = request.getParameter("pwd");
        String status = request.getParameter("status");
        UserBean userBean = new UserBean();
        User user= userBean.login(userid, pwd,status);
       
		try{
			//判断user是否为空
	        if (user != null) {
	        	HttpSession session=request.getSession();
	        	session.setAttribute("user", user);
	        	request.setAttribute("userid", userid);
	        	request.setAttribute("pwd", pwd);
	        	request.setAttribute("status", status);
	            ///根据用户选择的权限类型跳转页面，
	            if (status.equals("1"))
	                response.sendRedirect("index.jsp");
	           
	            else if (status.equals("2"))
	                response.sendRedirect("page/role/role_list.jsp");
	        	//request.getRequestDispatcher("index.jsp").forward(request, response);
	           
	        }else {
	            //登录失败
	        	request.setAttribute("info","用户名或密码错误！");
	        	request.getRequestDispatcher("error.jsp").forward(request, response);
	            
	        }

		}catch(Exception e){
			//e.printStackTrace();
			//path="error/500.jsp";
			logger.info(e.getMessage(),e);
			String path="login.jsp";
		}
	 }
	
}
